package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import domain.Category;
import domain.User;
public class USerDao extends BaseDao {
	public ArrayList<User> select (String sql) throws Exception{
		ArrayList<User> result = new ArrayList<User>();
		Connection conn = null ;
		PreparedStatement pstmt = null ;
		ResultSet rs = null ;
		conn = getConnection();
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		while(rs.next()){
			User obj = new User();			
			obj.setUsername(rs.getString("username"));
			obj.setPassword(rs.getString("password"));
			obj.setEmail(rs.getString("email"));
			obj.setBirthday(rs.getString("birthday"));
			result.add(obj);
		}
		rs.close();
		pstmt.close();
		conn.close();
		return result;
	}
	public void insert(User obj)throws Exception{
		Connection conn = null ;
		PreparedStatement ps = null ;
		String sql = "INSERT INTO login values(?,?)";
		conn = getConnection();
		ps = conn.prepareStatement(sql);
		ps.setString(1,obj.getUsername());
		ps.setString(2,obj.getPassword());	
		ps.execute();
		ps.close();
		conn.close();
	
	}
	public void update(User obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="update user set password=?,email=?,birthday=? where name=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getPassword());	
		ps.setString(2, obj.getEmail());
		ps.setString(4, obj.getUsername());
		ps.setString(3, obj.getBirthday());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	public void delete(User obj)throws Exception{
		Connection conn =null;
		PreparedStatement ps=null;
		conn=getConnection();
		String sql="delete from user where name=?";
		ps=conn.prepareStatement(sql);
		ps.setString(1, obj.getUsername());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	@SuppressWarnings("rawtypes")
	public User getUser(String userName,String password) throws Exception{
		User obj=null;
		ArrayList list=select("select*from user where username='"+userName+"' and password='"+password+"'");
		if(list.size()>0){
			obj=(User) list.get(0);
		}
		return obj;
	}
}
